<?php
class UserDao {
	private $table = 'user';
	public function searchList($where,$order='',$limit=0){
		$where = $this->searchWhere($where);
		return ClassManager::getSql()->dataList1($this->table,'*',$where,$order,$limit);
	}
	public function searchList1($where,$order='',$limit=0){
		$where = $this->searchWhere1($where);
		return ClassManager::getSql()->dataList1($this->table,'*',$where,$order,$limit);
	}
	public function getDataPairList($strs_list){
		$strs = implode(',', $strs_list);
		return ClassManager::getSql()->dataPairList($this->table,'id,english_name,skype_id,qq,phone,job,level,employee_id,email,user_kind '," where id in (".$strs.")");
	}
		
	public function searchNumber($where){
		$where = $this->searchWhere($where);
		$tmp = ClassManager::getSql()->onedata1($this->table,"count(*)  ",$where);
		return $tmp[0];
	}
	
	public function update($data,$id){
		ClassManager::getSql()->updates($this->table,$data,"where id=".intval($id));
	}
	public function updateInId($data,$id){
		$tmp  = implode(',',$id);
		ClassManager::getSql()->updates($this->table,$data,'where id in('.$tmp.')');
	}
	public function updateByEmail($data,$email){
		ClassManager::getSql()->updates($this->table,$data,"where email='".$email."'");
	}
	public function updateByPhone($data,$phone){
		ClassManager::getSql()->updates($this->table,$data,"where phone='".$phone."'");
	}

	public function get($id){
		return ClassManager::getSql()->oneDataCache($this->table, 'id',intval($id));
	}
	public function qqUserget($openid){
		return ClassManager::getSql()->oneDataCache($this->table, 'openid',$openid);
	}
	
	public function add($data){
		ClassManager::getSql()->insert($this->table,$data);
	}
	public function getByEmail($email){
		return  ClassManager::getSql()->oneDataCache($this->table,'email',$email);
	}
	public function getByPhone($phone){
		return ClassManager::getSql()->oneDataCache($this->table,'phone',$phone);
	}
	public function getByInviteUserId($user_id){
		return ClassManager::getSql()->oneDataCache($this->table,'user_id',intval($user_id));
	}
	public function deleteBatchByid($id){
		ClassManager::getSql()->delete($this->table,"where id in(".$id.")");
	}
	

	public function getSumUserRegister($where){
		$where = $this->searchMutilWhere($where);
		$sql = "select  count(1) cnt,user_type from eo_user  a where  1=1 ";
		$sql.= $where;
		$sql.=" group by a.user_type ";
		$sumRegister = ClassManager::getSql()->dataMutillist(' user and sumuserRegister ',$sql);
		
		return $sumRegister;
	}	


	public function getSumRegisterGroupByYearmonth($where){
		$where = $this->searchMutilWhere($where);
		$sql = "select  count(1) cnt,user_type,substr(from_unixtime(register_time),1,7) register_time from eo_user  a where  1=1 ";
		$sql.= $where;
		$sql.=" group by a.user_type,substr(from_unixtime(register_time),1,7) ";
		$sumRegister = ClassManager::getSql()->dataMutillist(' user and sumuserRegister ',$sql);
	
		return $sumRegister;
	}
	

	public function searchMutilList($where=array(),$limit=0){
		$where = $this->searchMutilWhere($where);
		$sql = "select distinct a.* from eo_user a left join eo_order b on a.id=b.user_id where 1=1 ";
		$sql.= $where;
		return ClassManager::getSql()->dataMutillist('user and order',$sql,$limit);
	}
	
	public function searchMutilWhere($where){
		$where1 = '';
		if(isset($where['user_send_type'])&&$where['user_send_type']==TICKET_SEND_TYPE_1){
			$where1 .=" and user_experience_class>0 and b.id is null";
		}
		if(isset($where['user_send_type'])&&$where['user_send_type']==TICKET_SEND_TYPE_2){
			$where1 .=" and user_experience_class=0 and b.id is null";
				
		}
		if(isset($where['user_send_type'])&&$where['user_send_type']==TICKET_SEND_TYPE_3){
			$where1 .="  and (user_class>0 or user_monthly_class>0)  ";
		}
	
		if(isset($where['user_send_type'])&&$where['user_send_type']==TICKET_SEND_TYPE_4){
			$where1 .=" and user_class=0 and user_monthly_class=0 and b.order_id>0 ";
		}
	
		if(isset($where['start_time'])&&$where['start_time']!=""){
			$where1 .=" and  a.register_time>='".strtotime($where['start_time'])."' ";
		}

		
		if(isset($where['over_time'])&&$where['over_time']!=""){
			$where1 .=" and  a.register_time<='".strtotime($where['over_time'])."' ";
		}
	
		if(isset($where['email'])&&$where['email']!=""){
			$where1 .=" and a.email = '".$where['email']."'";
		}
	
		if(isset($where['phone'])&&$where['phone']!=""){
			$where1 .=" and a.phone = '".$where['phone']."'";
		}
	
		return $where1;
	}
		
	
	public function searchWhere1($where){
		$likeWhere = array('english_name');
		if (!empty($where['start_time'])){
			$where['register_time'][] = array($where['start_time'],'>=');
		}
		if (!empty($where['over_time'])){
			$where['register_time'][] = array($where['over_time'],'<=');
		}
		foreach ($where as  $k=>$v){
			if (isset($v)&&$v!==""){
				if (in_array($k,$likeWhere) and !is_array($v)){
					$where1[$k] = array('%'.$v.'%' , 'like');
				}
				else{
					$where1[$k] =  $v;
				}
			}
		}
		return $where1;
	}
	
	public function searchWhere($where){
		$sql = " where 1=1 ";
		if (!empty($where['start_time'])){
			$sql.=" and register_time >=".strtotime($where['start_time']);
		}
		if (!empty($where['end_time'])){
			$end_time  = strtotime($where['end_time'])+86399;
			$sql.=" and register_time <=".$end_time;
		}
		if(isset($where['status'])&&$where['status']!==""){
			$sql.=" and status =".$where['status'];
		}
		
		if(isset($where['user_type'])&&$where['user_type']>0){
			$sql.=" and user_type ='".$where['user_type']."'";
		}
		
		if(isset($where['register_type'])&&$where['register_type']>0){
			$sql.=" and register_type ='".$where['register_type']."'";
		}
		
		if(isset($where['user_kind'])&&$where['user_kind']>0){
			$sql.=" and user_kind ='".$where['user_kind']."'";
		}
		
		if(isset($where['lead_source'])&&$where['lead_source']>0){
			$sql.=" and lead_source ='".$where['lead_source']."'";
		}

		if(isset($where['phone'])&&!empty($where['phone'])){
			$sql.=" and phone ='".$where['phone']."'";
		}

		if(isset($where['email'])&&!empty($where['email'])){
			$sql.=" and email ='".$where['email']."'";
		}

		if(isset($where['qq'])&&$where['qq']>0){
			$sql.=" and qq ='".$where['qq']."'";
		}

		if(isset($where['english_name'])&&$where['english_name']>0){
			$sql.=" and english_name ='".$where['english_name']."'";
		}
		
		if(isset($where['fund_user_id'])&&$where['fund_user_id']>0){
			$sql.=" and fund_user_id ='".$where['fund_user_id']."'";
		}
		
		if(isset($where['unfund_user_id'])&&$where['unfund_user_id']>0){
			$sql.=" and fund_user_id =0 ";
		}
		
		if(isset($where['qq_bind_status'])){
			$sql.=" and qq_bind_status = ".$where['qq_bind_status'];
		}
		
		if(isset($where['qq_bind_id'])&&$where['qq_bind_id']>0){
			$sql.=" and qq_bind_id = ".$where['qq_bind_id'];
		}
				
		if(isset($where['key_word'])&&$where['key_word']!==""){
			$sql.=" and (english_name like '%".$where['key_word']."%'";
			$sql.=" or phone like '%".$where['key_word']."%'";
			$sql.=" or email like '%".$where['key_word']."%'";
			$sql.=" or nickname like '%".$where['key_word']."%'";
			$sql.=" or qq like '%".$where['key_word']."%'";
			$sql.=" )";
		}
		
		if(isset($where['get_expire_user_monthly_class'])&&$where['get_expire_user_monthly_class']>0){
			if(isset($where['expire_time'])&&$where['expire_time']>0){
				$sql.=" and monthly_class_period_validity <= ".$where['expire_time']."";
				$sql.=" and  user_monthly_class >0 ";
			}
		}
		if(isset($where['get_expire_user_class'])&&$where['get_expire_user_class']>0){
			if(isset($where['expire_time'])&&$where['expire_time']>0){
				$sql.=" and class_period_validity <= ".$where['expire_time'];
				$sql.=" and  user_class >0  ";
			}
		}
		
		if(isset($where['user_id_list_str'])&&!empty($where['user_id_list_str'])){
				$sql.=" and id in  (".$where['user_id_list_str'].")";
		}
		
		return $sql;
	}
	

	public function getByEmailByPassword($email,$password){
		return ClassManager::getSql()->onedata('user','*',"where email='".$email."' and password='".md5($password)."' ");
	}
	public function batchUpdateTrackPeople($employeeId,$id){
		$data['employee_id'] = $employeeId ;
		ClassManager::getSql()->updates('user', $data,"where id in(".$id.")");
	}
}

?>